<?php

namespace app\index\controller;

use think\Controller;
use app\index\model\Showorder;
use app\index\model\Book;
use think\Db;
use app\index\model\Shoplist;
use app\index\model\Myorder;
use think\Request;

class Order extends Controller
{
    public function order()
    {
        if (empty(session('email'))) {
            $this->error('请先登录!', 'login/login');
        }

        $starttime = input('get.starttime');
        $endtime = input('get.endtime');
        $lowprice = input('get.lowprice');
        $ordertype = input('get.ordertype');
        $keyword = input('get.keyword');
        // dump($starttime);
        // dump($endtime);
        $str = "true ";
        if (!empty($starttime)) {
            $str .= ' and inputtime >= "' . $starttime . '"';
        }

        if (!empty($starttime)) {
            $str .= ' and inputtime <= "' . $endtime . '"';
        }
        if (!empty($lowprice)) {
            $str .= ' and shifu >= ' . $lowprice;
        }
        if (!empty($ordertype) && strcmp($ordertype, "不限")) {
            $str .= ' and STATUS = "' . $ordertype . '"';
        }
        //关键字
        if (!empty($keyword)) {
            //判断订单
            $str .= ' and exists (select * from showshoplist where showorder.orderID = showshoplist.orderID and bname like "%' . $keyword . '%")';
        }

        session("starttime", $starttime);
        session("endtime", $endtime);
        session("lowprice", $lowprice);
        session("ordertype", $ordertype);
        session("keyword", $keyword);
        // dump($str);

        $orders = Showorder::where('email', session('email'))->where($str)->order('orderID desc')->paginate(3);
        $page = $orders->render();
        $this->assign('showorder', $orders);
        $this->assign('page', $page);

        $orderlists = array();
        foreach ($orders as $order) {
            $shoplistitems = array();
            foreach ($order->showshoplist as $shoplist) {
                if ($order->orderID ==  $shoplist->orderID) {
                    array_push($shoplistitems, $shoplist);
                }
            }
            array_push($orderlists, $shoplistitems);
        }
        //   var_dump($orderlists);
        $this->assign('orderlists', $orderlists);
        return $this->fetch();
    }

    //删除订单
    public function delete()
    {
        // 事务处理开始
        Db::transaction(function () {

            $orderID = input('post.orderID');

            $shoplists = Shoplist::where('orderID', $orderID)->select();

            foreach ($shoplists as $shoplist) {
                $bookID = $shoplist->bookID;
                $num = $shoplist->num;

                $book = Book::get($bookID);
                $book->SelledNum -= $num;
                $book->storenum += $num;
                $book->save();

                $shoplist->delete();
            }
            $order = Myorder::get($orderID);
            $order->delete();
        });
        return "success";
    }


    //支付订单
    public function pay()
    {
        $orderID = input('get.orderID');
        $order = Myorder::get($orderID);
        $order->STATUS = '未发货';
        $order->cltime = date("Y-m-d H:i:s");
        $order->save();
        $this->redirect('order/order');
    }

    // 确认收货
    public function orderUpdate()
    {
        $orderID = input('get.orderID');
        $order = Myorder::get($orderID);
        $order->STATUS = '未评价';
        $order->cltime = date("Y-m-d H:i:s");
        $order->save();
        $this->redirect('order/order');
    }

    //添加订单 ----> 放到了forder-->addOrder


    //显示评价界面
    public function evaluate()
    {
        $orderID = input('get.orderID/d');
        $data = Db::table('showshoplist')->where('orderID', $orderID)->select();
        $this->assign('results', $data);
        return $this->fetch();
    }

    //评价处理
    public function doEvaluate(Request $request)
    {
        $orderID = input('post.orderID/d');
        $datas = Shoplist::where('orderID', $orderID)->select();
        foreach ($datas as $shoplist) {
            $SLID = $shoplist->SLID;
            $shoplist->email = session('email');
            $shoplist->pjcontent = $request->param('pjcontent' . $SLID);
            $shoplist->pjtime = date('Y-m-d H:i:s');
            $shoplist->save();
        }
        $order = Myorder::get($shoplist->orderID);
        $order->STATUS = '已完成';
        $order->cltime = date('Y-m-d H:i:s');
        $order->save();
        $this->redirect('order/order');
    }
}
